package com.zzk.lanouhn.myapplication1.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.ScrollRecyclerViewAdapter;
import com.zzk.lanouhn.myapplication1.modles.AllNews;
import com.zzk.lanouhn.myapplication1.view.ScrollRecyclerView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/8/25.
 */
public class NewsKetvFragment extends Fragment{

    private List<AllNews.Datas.DataBean> allNews=new ArrayList<>();;
    private ScrollRecyclerView scrollRecyclerView;
    private ScrollRecyclerViewAdapter adapter;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=LayoutInflater.from(getActivity()).inflate(R.layout.fragment_news_ketv,null);
        scrollRecyclerView= (ScrollRecyclerView) view.findViewById(R.id.scroll_ketv_news);
        getDataFromweb();
        return view;
    }

    private void getDataFromweb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url="https://rong.36kr.com/api/mobi/news?pageSize=20&columnId=tv&pagingAction=up";
                OkHttpClient client=new OkHttpClient();
                Request request=new Request.Builder()
                        .url(url)
                        .build();

                Call call=client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                      String result=response.body().string();
                        Gson gson=new Gson();
                        allNews=gson.fromJson(result,AllNews.class).getDatas().getData();
                        if (allNews.size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 0:
                    //Toast.makeText(getActivity(), "请求数据失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    adapter=new ScrollRecyclerViewAdapter(getActivity(),allNews);
                    scrollRecyclerView.setAdapter(adapter);
                    LinearLayoutManager manager=new LinearLayoutManager(getActivity());
                    manager.setOrientation(LinearLayoutManager.VERTICAL);
                    scrollRecyclerView.setLayoutManager(manager);
                    break;
              default:

                  break;
            }
        }
    };
}
